package com.qigao.mall.web.controller.app.user;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.qigao.mall.api.ItemService;
import com.qigao.mall.commons.dto.ItemQueryPageDto;
import com.qigao.mall.commons.dto.ItemResultDto;
import com.qigao.mall.commons.dto.SysDictResultDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.enums.StatusCharEnum;
import com.qigao.mall.web.vo.app.ItemQueryPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/app/user/item")
@Api(tags = "App商品相关API",value = "App商品相关API")
public class AppItemController {

    private static Logger logger= LoggerFactory.getLogger(AppItemController.class);
    @Resource
    ItemService itemService;

    //1.按照关键字进行商品搜索
    @PostMapping("/search/{shopNo}")
    @ApiOperation(value = "按照关键字进行已上架商品搜索", notes = "按照关键字进行商品搜索")
    public Result<PageInfo<ItemResultDto>> search(@PathVariable("shopNo")String shopNo,@RequestBody ItemQueryPageVo itemQueryPageVo){
        try{
            ItemQueryPageDto itemQueryPageDto=new ItemQueryPageDto();
            BeanUtils.copyProperties(itemQueryPageVo,itemQueryPageDto);
            itemQueryPageDto.setShopNo(shopNo);
            itemQueryPageDto.setStatus(StatusCharEnum.VALID.getCode());
            Page<ItemResultDto> itemResultDtoPage =itemService.search(itemQueryPageDto);
            if(itemResultDtoPage !=null && itemResultDtoPage.getResult().size()>=1){
                PageInfo<ItemResultDto> dictResultDtoPageInfo = new PageInfo(itemResultDtoPage);
               return Result.newSuccess(dictResultDtoPageInfo);
            }
            return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
        }catch (Exception e){
            logger.error("[商品搜索]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    //5.按照商品编号查询商品信息
    @GetMapping("/findGoodsByItemNo/{itemNo}")
    @ApiOperation(value = "按照商品编号查询商品信息", notes = "按照商品编号查询商品信息")
    public Result<ItemResultDto> findGoodsByItemNo(@PathVariable("itemNo")String itemNo){
        try{
            //参数校验
            return itemService.findGoodsByItemNo(itemNo);
        }catch (Exception e){
            logger.error("[按照商品编号查询商品信息]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    /**
     * 按照分类显示商品
     * @return
     */
    @GetMapping("/showItemByCatNo")
    @ApiOperation(value = "按照分类显示已上架商品", notes = "按照分类显示已上架商品")
    public Result<List<ItemResultDto>> showItemByCatNo(@RequestParam("catNo")String catNo){
        try{
            List<ItemResultDto> itemResultDtos =itemService.showItemByCatNo(catNo,"1");
            if(itemResultDtos !=null && itemResultDtos.size()>=1){
                return Result.newSuccess(itemResultDtos);
            }
            return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
        }catch (Exception e){
            logger.error("[商品分类]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    @GetMapping("/randomItem/{shopNo}")
    @ApiOperation(value = "随机显示已上架商品", notes = "随机显示已上架商品")
    public Result<List<ItemResultDto>> randomItem(@PathVariable("shopNo")String shopNo){
        try{
            List<ItemResultDto> itemResultDtos =itemService.randomItem(shopNo);
            if(itemResultDtos !=null && itemResultDtos.size()>=1){
                return Result.newSuccess(itemResultDtos);
            }
            return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
        }catch (Exception e){
            logger.error("[商品搜索]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }



}
